# Find and sets up RobWorkHardware
#
# Output variables: ROBWORKHARDWARE_INCLUDE_DIR  - Where to find RobWork include sub-directory.
# ROBWORKHARDWARE_LIBRARIES    - List of libraries when using RobWork (includes all libraries that
# RobWork depends on). ROBWORKHARDWARE_LIBRARY_DIRS - List of directories where libraries of RobWork
# are located. ROBWORKHARDWARE_FOUND        - True if RobWork was found. (not fully implemented yet)
#
# ROBWORKHARDWARE_ROOT         - If set this defines the root of RobWorkHardware if not set then it
# if possible be autodetected.
#
# Input variables: RobWorkHardware_FIND_COMPONENTS - List of required RWHW components to search for
# (see list below). If REQUIRED is set then a fatal error is reported if these are not found. else
# only a warning is issues.
#
# Components: camera can crsa465 dockwelder fanucdevice katana motomanIA20 netft pa10 pcube
# schunkpg70 sdh serialport sick swissranger tactile trakstar universalrobots

# ---[ Find RobWorkHardware

if(ROBWORKHARDWARE_FIND_QUIETLY)
    set(QUIET_ QUIET)
else(ROBWORKHARDWARE_FIND_QUIETLY)
    set(QUIET_)
endif(ROBWORKHARDWARE_FIND_QUIETLY)

# ############################################# MACROS
# ##################################################################################################

# macro for determining the best RobWork build type match
macro(GET_ROBWORKHARDWARE_BUILD_TYPE CFG_ROOT RWHW_BUILD_TYPE)
    # defaults to release
    set(BTYPE_TMP release)
    if(CMAKE_BUILD_TYPE)
        string(TOLOWER ${CMAKE_BUILD_TYPE} BTYPE_TMP)
    else()
        set(BTYPE_TMP "none")
    endif()

    # first test if the correct cmake build type is installed
    if(EXISTS ${CFG_ROOT}/RobWorkHardwareBuildConfig_${BTYPE_TMP}.cmake)
        set(${RWHW_BUILD_TYPE} ${BTYPE_TMP})
    else()

        # find best robworkhardware build match
        if(${BTYPE_TMP} STREQUAL "release")
            # find release compatible robworkhardware installation
            if(EXISTS ${CFG_ROOT}/RobWorkHardwareBuildConfig_minsizerel.cmake)
                set(${RWHW_BUILD_TYPE} minsizerel)
            elseif(EXISTS ${CFG_ROOT}/RobWorkHardwareBuildConfig_relwithdebinfo.cmake)
                set(${RWHW_BUILD_TYPE} relwithdebinfo)
            elseif(EXISTS ${CFG_ROOT}/RobWorkHardwareBuildConfig_debug.cmake)
                set(${RWHW_BUILD_TYPE} debug)
            elseif(EXISTS ${CFG_ROOT}/RobWorkHardwareBuildConfig_none.cmake)
                set(${RWHW_BUILD_TYPE} none)
            else()
                message(FATAL_ERROR "Could not find any build of RobWorkHardware!")
            endif()
        elseif(${BTYPE_TMP} STREQUAL "minsizerel")
            if(EXISTS ${CFG_ROOT}/RobWorkHardwareBuildConfig_release.cmake)
                set(${RWHW_BUILD_TYPE} release)
            elseif(EXISTS ${CFG_ROOT}/RobWorkHardwareBuildConfig_relwithdebinfo.cmake)
                set(${RWHW_BUILD_TYPE} relwithdebinfo)
            elseif(EXISTS ${CFG_ROOT}/RobWorkHardwareBuildConfig_debug.cmake)
                set(${RWHW_BUILD_TYPE} debug)
            elseif(EXISTS ${CFG_ROOT}/RobWorkHardwareBuildConfig_none.cmake)
                set(${RWHW_BUILD_TYPE} none)
            else()
                message(FATAL_ERROR "Could not find any build of RobWorkHardware!")
            endif()
        elseif(${BTYPE_TMP} STREQUAL "relwithdebinfo")
            if(EXISTS ${CFG_ROOT}/RobWorkHardwareBuildConfig_release.cmake)
                set(${RWHW_BUILD_TYPE} release)
            elseif(EXISTS ${CFG_ROOT}/RobWorkHardwareBuildConfig_minsizerel.cmake)
                set(${RWHW_BUILD_TYPE} minsizerel)
            elseif(EXISTS ${CFG_ROOT}/RobWorkHardwareBuildConfig_debug.cmake)
                set(${RWHW_BUILD_TYPE} debug)
            elseif(EXISTS ${CFG_ROOT}/RobWorkHardwareBuildConfig_none.cmake)
                set(${RWHW_BUILD_TYPE} none)
            else()
                message(FATAL_ERROR "Could not find any build of RobWorkHardware!")
            endif()
        elseif(${BTYPE_TMP} STREQUAL "debug")
            if(EXISTS ${CFG_ROOT}/RobWorkHardwareBuildConfig_relwithdebinfo.cmake)
                set(${RWHW_BUILD_TYPE} relwithdebinfo)
            elseif(EXISTS ${CFG_ROOT}/RobWorkHardwareBuildConfig_minsizerel.cmake)
                set(${RWHW_BUILD_TYPE} minsizerel)
            elseif(EXISTS ${CFG_ROOT}/RobWorkHardwareBuildConfig_release.cmake)
                set(${RWHW_BUILD_TYPE} release)
            elseif(EXISTS ${CFG_ROOT}/RobWorkHardwareBuildConfig_none.cmake)
                set(${RWHW_BUILD_TYPE} none)
            else()
                message(FATAL_ERROR "Could not find any build of RobWorkHardware!")
            endif()
        elseif(${BTYPE_TMP} STREQUAL "none")
            if(EXISTS ${CFG_ROOT}/RobWorkHardwareBuildConfig_relwithdebinfo.cmake)
                set(${RWHW_BUILD_TYPE} relwithdebinfo)
            elseif(EXISTS ${CFG_ROOT}/RobWorkHardwareBuildConfig_minsizerel.cmake)
                set(${RWHW_BUILD_TYPE} minsizerel)
            elseif(EXISTS ${CFG_ROOT}/RobWorkHardwareBuildConfig_release.cmake)
                set(${RWHW_BUILD_TYPE} release)
            elseif(EXISTS ${CFG_ROOT}/RobWorkHardwareBuildConfig_debug.cmake)
                set(${RWHW_BUILD_TYPE} debug)
            else()
                message(FATAL_ERROR "Could not find any build of RobWorkHardware!")
            endif()
        else()
            message(
                STATUS
                    "Does not recognize build type: ${CMAKE_BUILD_TYPE} choosing any existing RobWorkHardware installation!"
            )
            if(EXISTS ${CFG_ROOT}/RobWorkHardwareBuildConfig_release.cmake)
                set(${RWHW_BUILD_TYPE} release)
            elseif(EXISTS ${CFG_ROOT}/RobWorkHardwareBuildConfig_minsizerel.cmake)
                set(${RWHW_BUILD_TYPE} minsizerel)
            elseif(EXISTS ${CFG_ROOT}/RobWorkHardwareBuildConfig_relwithdebinfo.cmake)
                set(${RWHW_BUILD_TYPE} relwithdebinfo)
            elseif(EXISTS ${CFG_ROOT}/RobWorkHardwareBuildConfig_debug.cmake)
                set(${RWHW_BUILD_TYPE} debug)
            elseif(EXISTS ${CFG_ROOT}/RobWorkHardwareBuildConfig_none.cmake)
                set(${RWHW_BUILD_TYPE} none)
            else()
                message(FATAL_ERROR "Could not find any build of RobWorkHardware!")
            endif()

        endif()
        message(
            STATUS
                "warning: RobWorkHardware was not compiled with type:${BTYPE_TMP} using type:${${RWHW_BUILD_TYPE}} instead!"
        )
    endif()

endmacro()

set(RWHWCFG_ROOT ${CMAKE_CURRENT_LIST_DIR})

# check if user specified a RobWorkHardware_DIR
if(DEFINED RobWorkHardware_DIR)
    if(EXISTS "${RobWorkHardware_DIR}/../src/RobWorkHardwareConfig.hpp")
        # Found RobWorkHardware in a build tree of RobWork
        set(RWHWCFG_ROOT "${RobWorkHardware_DIR}")
        set(is_installed false)
    elseif(EXISTS "${RobWorkHardware_DIR}/RobWorkHardwareConfig.cmake")
        # Found a RobWorkHardware installation
        set(RWHWCFG_ROOT "${RobWorkHardware_DIR}")
        set(is_installed true)
    elseif(
        EXISTS
        "${RobWorkHardware_DIR}/include/robworkhardware-${RobWorkHardware_VERSION_MAJOR}.${RobWorkHardware_VERSION_MINOR}/RobWorkHardwareConfig.hpp"
    )
        set(
            RWHWCFG_ROOT
            "${RobWorkHardware_DIR}/share/robworkhardware-${RobWorkHardware_VERSION_MAJOR}.${RobWorkHardware_VERSION_MINOR}/cmake"
        )
        set(is_installed true)
    else()
        # found no RobWorkHardware installation ot build tree in RobWorkHardware_DIR so we try
        # extracting it from RobWorkHardwareConfig.cmake location instead
    endif()
    get_filename_component(RWHWCFG_ROOT ${RWHWCFG_ROOT} ABSOLUTE)
endif()

# get the relavant build type
get_robworkhardware_build_type(${RWHWCFG_ROOT} RWHW_BUILD_TYPE)

set(TARGET_EXISTS False)
foreach(
    _expectedTarget
    sdurwhw_camera
    sdurwhw_can
    sdurwhw_dockwelder
    sdurwhw_pcube
    sdurwhw_serialport
    sdurwhw_tactile
    sdurwhw_motomanIA20
    sdurwhw_universalrobots
    sdurwhw_universalrobots_rtde
    sdurwhw_schunkpg70
    sdurwhw_netft
    sdurwhw_robolabFT
    sdurwhw_robotiq
)
    if(TARGET ${_expectedTarget})
        set(TARGET_EXISTS True)
    endif()

endforeach()

if(NOT TARGET_EXISTS)
    include("${RWHWCFG_ROOT}/RobWorkHardwareTargets.cmake")
endif()

# check whether RobWorkConfig.cmake is found into a RobWork installation or in a build tree
if(EXISTS "${RWHWCFG_ROOT}/../src/RobWorkHardwareConfig.hpp")
    # Found RobWorkConfig.cmake in a build tree of RobWork
    set(succmsg "RobWorkHardware: Found a RobWorkHardware build tree")
    set(RWHW_ROOT "${RWHWCFG_ROOT}/..")

    set(RWHW_INCLUDE_EXT "${RWHW_ROOT}/ext")
    set(RWHW_INCLUDE_SRC "${RWHW_ROOT}/src/")
    set(RWHW_LIBS "${RWHW_ROOT}/libs/${RWHW_BUILD_TYPE}/")
    set(IS_INSTALL FALSE)
else()
    set(succmsg "RobWorkHardware: Found a RobWorkHardware installation")
    set(IS_INSTALL TRUE)
    # Found a RobWork installation
    if(WIN32)
        # RobWorkConfig.cmake is installed to RWHW_ROOT/cmake
        set(RWHW_ROOT "${RWHWCFG_ROOT}/../..")
        set(RWHW_INCLUDE_EXT "${RWHW_ROOT}/../ext")
        set(
            RWHW_INCLUDE_SRC
            "${RWHW_ROOT}/include/robworkhardware-${RobWorkHardware_VERSION_MAJOR}.${RobWorkHardware_VERSION_MINOR}"
        )
        set(RWHW_LIBS "${RWHW_ROOT}/lib")
    else()
        # RobWorkConfig.cmake is installed to RWHW_INTALL/share/robworkhardware-x.y

        set(RWHW_ROOT "${RWHWCFG_ROOT}")
        set(RWHW_INSTALL "${RWHWCFG_ROOT}/../../..")
        set(RWHW_LIBS "${RWHW_INSTALL}/lib/")
        set(
            RWHW_INCLUDE_SRC
            "${RWHW_INSTALL}/include/robworkhardware-${RobWorkHardware_VERSION_MAJOR}.${RobWorkHardware_VERSION_MINOR}"
        )
        set(
            RWHW_INCLUDE_EXT
            "${RWHW_INSTALL}/include/robworkhardware-${RobWorkHardware_VERSION_MAJOR}.${RobWorkHardware_VERSION_MINOR}/ext"
        )
    endif(WIN32)
endif()

# MESSAGE(STATUS "RWHW_ROOT   : ${RWHW_ROOT}") MESSAGE(STATUS "RWHWCFG_ROOT: ${RWHWCFG_ROOT}")

# ##################################################################################################
# now RWHW_ROOT and RWHWCFG_ROOT is set. Lets extract the stuff needed to run a project

# next get the build configuration of the requested built type
if(EXISTS ${RWHWCFG_ROOT}/RobWorkHardwareBuildConfig_${RWHW_BUILD_TYPE}.cmake)
    include(${RWHWCFG_ROOT}/RobWorkHardwareBuildConfig_${RWHW_BUILD_TYPE}.cmake)

    list(APPEND CMAKE_MODULE_PATH ${RWHWCFG_ROOT}/Modules)

    # Find RobWork
    if(NOT DEFINED RobWork_FOUND OR NOT ${RobWork_FOUND})
        if(${IS_INSTALL})
            find_package(RobWork QUIET)
        else()
            set(RobWork_FOUND FALSE)
        endif()

        if(NOT ${RobWork_FOUND})
            find_package(RobWork REQUIRED HINTS ${RWHW_BUILD_WITH_RW_ROOT}/cmake)
        endif()
    else()
        message(STATUS "RobWork Already Found")
    endif()

    set(BOOST_ROOT ${RWHW_BUILD_WITH_BOOST_ROOT})
    set(BOOST_INCLUDEDIR ${RWHW_BUILD_WITH_BOOST_INCLUDE_DIR})
    set(BOOST_LIBRARYDIR ${RWHW_BUILD_WITH_BOOST_LIBRARY_DIR})

    if(DEFINED WIN32 AND ${IS_INSTALL})
        set(BOOST_INCLUDEDIR "${RW_INCLUDE_EXT}/boost")
        set(BOOST_LIBRARYDIR "${RW_LIBS}")
    endif()

    # TODO for each component that is enabled we might need to include additional stuff

    set(LIBRARIES_TO_INCLUDE) # Libraries that must be included
    set(OPTIONAL_LIBRARIES_TO_INCLUDE) # libraries that are included if they have been build
    if(RobWorkHardware_FIND_COMPONENTS)
        # FIRST check if all required components are installed/build
        foreach(component IN LISTS RobWorkHardware_FIND_COMPONENTS)
            set(FILTER "sdurwhw_${component}")
            string(REGEX MATCH ${FILTER} RESULT "${RWHW_BUILD_WITH_LIBRARIES}")

            unset(FOUND CACHE)
            if(RESULT)
                message(STATUS "Found in list: ${RESULT}")
                find_library(
                    FOUND ${RESULT}
                    PATHS ${RWHW_LIBS}
                    PATH_SUFFIXES ${RWHW_BUILD_WITH_LIBRARY_SUBDIRS}
                    NO_DEFAULT_PATH
                )
            endif()

            if(FOUND)
                list(APPEND LIBRARIES_TO_INCLUDE "sdurwhw_${component}")
                set(RobWorkHardware_${component}_FOUND TRUE)
            else()
                message("component not found: ${component}")
                set(RobWorkHardware_${component}_FOUND FALSE)
            endif()
        endforeach()
    else()
        foreach(lib IN LISTS RW_BUILD_WITH_LIBRARIES)
            unset(FOUND CACHE)
            find_library(
                FOUND ${lib}
                PATHS ${RWHW_LIBS}
                PATH_SUFFIXES ${RWHW_BUILD_WITH_LIBRARY_SUBDIRS} NO_DEFAULT_PATH
            )
            if(FOUND)
                list(APPEND OPTIONAL_LIBRARIES_TO_INCLUDE "${lib}")
                rw_message("Looking for ${lib} - found")
            else()
                if(${lib} STREQUAL "sdurw")
                    list(APPEND OPTIONAL_LIBRARIES_TO_INCLUDE "${lib}")
                    rw_message("Looking for ${lib} - not found")
                else()
                    rw_message("Looking for ${lib} - not found (ignored)")
                endif()

            endif()
        endforeach()
    endif()

    # SDH
    list(FIND OPTIONAL_LIBRARIES_TO_INCLUDE "sdurwhw_sdh" USE_SDH)
    list(FIND LIBRARIES_TO_INCLUDE "sdurwhw_sdh" MUST_USE_SDH)
    if(${USE_SDH} GREATER -1 OR ${MUST_USE_SDH} GREATER -1)
        find_package(SDH)
        if(SDH_FOUND)
            message(STATUS "RobWork Hardware: component sdurwhw_sdh is included!")
            list(APPEND OPTIONAL_LIBRARIES_TO_INCLUDE ${SDH_LIBRARY})
            list(APPEND ROBWORKHARDWARE_INCLUDE_DIRS_TMP ${SDH_INCLUDE_DIR})
        else()
            message(STATUS "RobWork Hardware: component sdurwhw_sdh is NOT included!")
            list(FIND LIBRARIES_TO_INCLUDE "sdurwhw_sdh" FORCE_SDH)
            if(FORCE_SDH)
                message(SEND_ERROR "SDH requirements could not be resolved!")
            endif()
        endif()
    endif()

    list(FIND OPTIONAL_LIBRARIES_TO_INCLUDE "sdurwhw_trakstar" USE_TRAKSTAR)
    list(FIND LIBRARIES_TO_INCLUDE "sdurwhw_trakstar" MUST_USE_TRAKSTAR)
    if(${USE_TRAKSTAR} GREATER -1 OR ${MUST_USE_TRAKSTAR} GREATER -1)
        find_package(TrakStar)
        if(TRAKSTAR_FOUND)
            message(STATUS "RobWork Hardware: component sdurwhw_sdh is included!")
            list(APPEND OPTIONAL_LIBRARIES_TO_INCLUDE ${TRAKSTAR_LIBRARY})
            list(APPEND ROBWORKHARDWARE_INCLUDE_DIRS_TMP ${TRAKSTAR_INCLUDE_DIR})
        elseif()
            message(STATUS "RobWork Hardware: component sdurwhw_trakstar is NOT included!")
            list(FIND LIBRARIES_TO_INCLUDE "sdurwhw_trakstar" FORCE_TRAKSTAR)
            if(FORCE_TRAKSTAR)
                message(SEND_ERROR "TRAKSTAR requirements could not be resolved!")
            endif()
            # remove lib from RWHW_REMOVE_INTERNAL_LIBRARY("sdurwhw_trakstar")
        endif()
    endif()

    list(FIND OPTIONAL_LIBRARIES_TO_INCLUDE "sdurwhw_can" USE_CAN)
    list(FIND LIBRARIES_TO_INCLUDE "sdurwhw_can" MUST_USE_CAN)
    if(${USE_CAN} GREATER -1 OR ${MUST_USE_CAN} GREATER -1)
        if(CMAKE_COMPILER_IS_GNUCXX)
            if(DEFINED MINGW)
                # TODO mingw32 libraries
                find_package(IEICAN)
                if(IEICAN_FOUND)
                    message(STATUS "RobWork Hardware: component sdurwhw_can - IEICAN is included!")
                    add_definitions(-DRWHW_HAS_IEICAN)
                    list(
                        APPEND
                            ROBWORKHARDWARE_INCLUDE_DIRS_TMP ${ROBWORKHARDWARE_INCLUDE_DIRS}
                            ${IEICAN_INCLUDE_DIR}
                    )
                else()
                    message(
                        STATUS "RobWork Hardware: component sdurwhw_can - IEICAN is NOT included!"
                    )
                endif()
            else()
                message(STATUS "RobWork Hardware: component sdurwhw_can - IEICAN is NOT included!")
            endif()
        elseif(DEFINED MSVC)
            # TODO MSVC
            find_package(IEICAN)
            if(IEICAN_FOUND)
                message(STATUS "RobWork Hardware: component sdurwhw_can - IEICAN is NOT included!")
                add_definitions(-DRWHW_HAS_IEICAN)
                list(
                    APPEND
                        ROBWORKHARDWARE_INCLUDE_DIRS_TMP ${ROBWORKHARDWARE_INCLUDE_DIRS}
                        ${IEICAN_INCLUDE_DIR}
                )
            else()
                message(STATUS "RobWork Hardware: component sdurwhw_can - IEICAN is NOT included!")
            endif()
        endif()

        # ESDCAN
        find_package(ESDCAN)
        if(ESDCAN_FOUND)
            message(STATUS "RobWork Hardware: component sdurwhw_can - ESDCAN is included!")
            add_definitions(-DRWHW_HAS_ESDCAN)
            list(APPEND OPTIONAL_LIBRARIES_TO_INCLUDE ${ESDCAN_LIBRARY})
            list(APPEND ROBWORKHARDWARE_INCLUDE_DIRS_TMP ${ESDCAN_INCLUDE_DIR})
        else()
            message(STATUS "RobWork Hardware: component sdurwhw_can - ESDCAN is NOT included!")
        endif()
    endif()

    list(FIND OPTIONAL_LIBRARIES_TO_INCLUDE "sdurwhw_camera" USE_CAMERA)
    list(FIND LIBRARIES_TO_INCLUDE "sdurwhw_camera" MUST_USE_CAMERA)
    if(${USE_CAMERA} GREATER -1 OR ${MUST_USE_CAMERA} GREATER -1)
        if(CMAKE_COMPILER_IS_GNUCXX)
            if(DEFINED MINGW)
                # TODO mingw32 libraries
            else()
                find_package(DC1394)
                if(DC1394_FOUND)
                    message(
                        STATUS "RobWork Hardware: component sdurwhw_camera - DC1394 is included!"
                    )
                    list(APPEND OPTIONAL_LIBRARIES_TO_INCLUDE ${DC1394_LIBRARY})
                    list(APPEND ROBWORKHARDWARE_INCLUDE_DIRS_TMP ${DC1394_INCLUDE_DIR})
                else()
                    message(
                        STATUS
                            "RobWork Hardware: component sdurwhw_camera - DC1394 is NOT included!"
                    )
                endif()
            endif()
        elseif(DEFINED MSVC)
            # TODO MSVC AND CMU1394
        endif()
    endif()

    list(FIND OPTIONAL_LIBRARIES_TO_INCLUDE "sdurwhw_universalrobots_rtde" USE_UR_RTDE)
    list(FIND LIBRARIES_TO_INCLUDE "sdurwhw_universalrobots_rtde" MUST_USE_UR_RTDE)
    if(${USE_UR_RTDE} GREATER -1 OR ${MUST_USE_UR_RTDE} GREATER -1)  
        find_package(ur_rtde QUIET)
        if(NOT ur_rtde_FOUND)
            set(UR_RTDE_PATH)
            if(NOT IS_INSTALL)
                set(UR_RTDE_PATH "${RWHW_BUILD_WITH_UR_RTDE}")
            elseif(WIN32 AND IS_INSTALL)
                set(UR_RTDE_PATH "${RWHW_LIBS}/cmake")
            endif()
            find_package(ur_rtde REQUIRED PATHS ${UR_RTDE_PATH})
        endif()

        if(WIN32 AND IS_INSTALL)
            #find static filepath properties
            get_target_property(I_DIR ur_rtde::rtde INTERFACE_INCLUDE_DIRECTORIES)
            get_target_property(L_LIBS ur_rtde::rtde INTERFACE_LINK_LIBRARIES)

            #find boost path to be replaced
            set(boost_DIR ${I_DIR})
            list(FILTER boost_DIR INCLUDE REGEX "/boost.*")
            get_filename_component(MOD_ROOT ${boost_DIR} NAME)
            set(regex "${boost_DIR}\/[a-z0-9.-]*")
            string(REGEX MATCH "${regex}" boost_LIB_DIR ${L_LIBS})

            #replace boost path
            string(REPLACE "${boost_DIR}" "${BOOST_INCLUDEDIR}" I_DIR "${I_DIR}")
            string(REPLACE "${boost_LIB_DIR}" "${BOOST_LIBRARYDIR}" L_LIBS "${L_LIBS}")
            set_target_properties(ur_rtde::rtde PROPERTIES
                INTERFACE_INCLUDE_DIRECTORIES "${I_DIR}"
                INTERFACE_LINK_LIBRARIES  "${L_LIBS}"
                )
        endif()
        list(APPEND OPTIONAL_LIBRARIES_TO_INCLUDE ur_rtde::rtde)
    endif()

    # Set extra compiler flags. The user should be able to change this
    set(
        RWHW_CXX_FLAGS
        ${RWHW_BUILD_WITH_CXX_FLAGS}
        CACHE STRING "Change this to force using your own
                    flags and not those of RobWorkHardware"
    )
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${RWHW_CXX_FLAGS}")

    # Set extra linker flags. The user should be able to change this
    set(
        RWHW_LINKER_FLAGS
        ${RWHW_BUILD_WITH_LINKER_FLAGS}
        CACHE STRING "Change this to force using your own linker
                    flags and not those of RobWork"
    )
    set(
        CMAKE_SHARED_LINKER_FLAGS
        "${CMAKE_SHARED_LINKER_FLAGS} ${RWHW_LINKER_FLAGS}"
        CACHE STRING "" FORCE
    )
    set(
        CMAKE_MODULE_LINKER_FLAGS
        "${CMAKE_MODULE_LINKER_FLAGS} ${RWHW_LINKER_FLAGS}"
        CACHE STRING "" FORCE
    )
    if(WIN32)
        set(
            CMAKE_EXE_LINKER_FLAGS
            "${CMAKE_EXE_LINKER_FLAGS} ${RWHW_LINKER_FLAGS}"
            CACHE STRING "" FORCE
        )
    endif()

    set(ROBWORKHARDWARE_BUILD_PATH "${RWHW_BUILD_WITH_RWHW_ROOT}")
    set(ROBWORKHARDWARE_INCLUDE_DIRS_TMP "${ROBWORKHARDWARE_INCLUDE_DIRS_TMP}"
                                         "${RWHW_BUILD_WITH_INCLUDE_DIR}" "${ROBWORK_INCLUDE_DIRS}")
    set(ROBWORKHARDWARE_LIBRARY_DIRS_TMP "${ROBWORKHARDWARE_LIBRARY_DIRS_TMP}"
                                         "${RWHW_BUILD_WITH_LIBRARY_DIRS}"
                                         "${ROBWORK_LIBRARY_DIRS}")
    set(ROBWORKHARDWARE_LIBRARIES_TMP "${LIBRARIES_TO_INCLUDE}" "${OPTIONAL_LIBRARIES_TO_INCLUDE}"
                                      "${ROBWORK_LIBRARIES}")

    # make sure that the library and include paths are pointing to the right locations
    string(
        REPLACE
            "${ROBWORKHARDWARE_BUILD_PATH}/ext" "${RWHW_INCLUDE_EXT}" ROBWORKHARDWARE_INCLUDE_DIRS
            "${ROBWORKHARDWARE_INCLUDE_DIRS_TMP}"
    )
    string(
        REPLACE
            "${ROBWORKHARDWARE_BUILD_PATH}/src" "${RWHW_INCLUDE_SRC}" ROBWORKHARDWARE_INCLUDE_DIRS
            "${ROBWORKHARDWARE_INCLUDE_DIRS}"
    )
    list(REMOVE_DUPLICATES ROBWORKHARDWARE_INCLUDE_DIRS)

    string(
        REPLACE
            "${ROBWORKHARDWARE_BUILD_PATH}/libs/${RWHW_BUILD_TYPE}" "${RWHW_LIBS}"
            ROBWORKHARDWARE_LIBRARY_DIRS "${ROBWORKHARDWARE_LIBRARY_DIRS_TMP}"
    )
    list(REMOVE_DUPLICATES ROBWORKHARDWARE_LIBRARY_DIRS)

    string(
        REPLACE
            "${ROBWORKHARDWARE_BUILD_PATH}/libs/${RWHW_BUILD_TYPE}" "${RWHW_LIBS}"
            ROBWORKHARDWARE_LIBRARIES "${ROBWORKHARDWARE_LIBRARIES_TMP}"
    )
    list(REMOVE_DUPLICATES ROBWORKHARDWARE_LIBRARIES)

else()
    message(STATUS "RobWorkHardware: Could not find a suitable RobWorkHardware installation!")
endif()

# Find and add full path information for the RobWorkHardware libraries
set(ROBWORKHARDWARE_LIBRARIES_TMP ${ROBWORKHARDWARE_LIBRARIES})
set(ROBWORKHARDWARE_LIBRARIES)
foreach(l ${ROBWORKHARDWARE_LIBRARIES_TMP})
    unset(tmp CACHE)
    find_library(tmp ${l} PATHS ${RWHW_LIBS} PATH_SUFFIXES ${RWHW_BUILD_WITH_LIBRARY_SUBDIRS} NO_DEFAULT_PATH)
    if(tmp)
        list(APPEND ROBWORKHARDWARE_LIBRARIES ${tmp})
    else()
        list(APPEND ROBWORKHARDWARE_LIBRARIES ${l})
    endif()
endforeach(l)

include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(
    RobWorkHardware
    REQUIRED_VARS
    RWHW_ROOT
    ROBWORKHARDWARE_LIBRARIES
    ROBWORKHARDWARE_INCLUDE_DIRS
    ROBWORKHARDWARE_LIBRARY_DIRS
    VERSION_VAR
    ${RobWorkHardware_VERSION}
    HANDLE_COMPONENTS
)
mark_as_advanced(ROBWORKHARDWARE_LIBRARIES ROBWORKHARDWARE_INCLUDE_DIRS
                 ROBWORKHARDWARE_LIBRARY_DIRS)

if(ROBWORKHARDWARE_FOUND)
    set(ROBWORKHARDWARE_VERSION ${RobWorkHardware_VERSION} CACHE STRING "RobWorkHardware version")
endif(ROBWORKHARDWARE_FOUND)
